
test={
    "1": {
        "base_info": {
            "index": "10",
            "vue_name": "a_1_0_2_28_1",
            "deepcopy_data": {
                "index": 1,
                "en_name": "ardu_time_control",
                "ch_name": "时间控制库",
                "note": "",
                "name": "ardu_time_control[时间控制库]",
                "ps": {
                    "1": {
                        "k": "params1",
                        "v": "value1"
                    }
                },
                "deepcopy_data": {
                    "name": "时间控制库"
                }
            }
        },
        "dict": {
            "1": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "id",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "定时命令id",
                        "index_type": "PRIMARY KEY",
                        "note": "",
                        "name": "id[定时命令id]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        },
                        "deepcopy_data": {
                            "name": "灌溉阀的id"
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "2": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "do_id",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "灌溉阀的id",
                        "index_type": "None",
                        "note": "",
                        "name": "do_id[灌溉阀的id]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "3": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "start_time",
                        "field_type": "TEXT",
                        "default_value": "",
                        "ch_name": "开始控制时间",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "关闭控制的时间"
                        },
                        "name": "start_time[开始控制时间]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "4": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "end_time",
                        "field_type": "TEXT",
                        "default_value": "",
                        "ch_name": "关闭控制的时间",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "预计消耗时间"
                        },
                        "name": "end_time[关闭控制的时间]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "5": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "time_size",
                        "field_type": "TEXT",
                        "default_value": "",
                        "ch_name": "预计消耗时间",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "控制条件"
                        },
                        "name": "time_size[预计消耗时间]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "6": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "if",
                        "field_type": "TEXT",
                        "default_value": "",
                        "ch_name": "控制条件",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "关闭控制的时间"
                        },
                        "name": "if[控制条件]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "7": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "if_on",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "是否启用控制条件",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "控制条件"
                        },
                        "name": "if_on[是否启用控制条件]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "8": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "id_on",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "是否启用该控制",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "是否启用控制条件"
                        },
                        "name": "id_on[是否启用该控制]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "9": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "next_time",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "下一次执行的开始时间",
                        "index_type": "None",
                        "note": "",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        },
                        "deepcopy_data": {
                            "name": "开始控制时间"
                        },
                        "name": "next_time[下一次执行的开始时间]"
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "10": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "interval_time",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "执行的间隔时间",
                        "index_type": "None",
                        "note": "0代表每天，1代表隔一天执行一次",
                        "deepcopy_data": {
                            "name": "开始控制时间"
                        },
                        "name": "interval_time[执行的间隔时间]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            }
        }
    },
    "2": {
        "base_info": {
            "index": "6",
            "vue_name": "a_1_0_2_28_1",
            "deepcopy_data": {
                "index": 1,
                "en_name": "ardu_do",
                "ch_name": "灌溉区库",
                "note": "",
                "ps": {
                    "1": {
                        "k": "params1",
                        "v": "value1"
                    }
                },
                "name": "ardu_do[灌溉区库]"
            }
        },
        "dict": {
            "1": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "id",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "灌溉阀的id",
                        "index_type": "PRIMARY KEY",
                        "note": "",
                        "deepcopy_data": {
                            "name": "执行的间隔时间"
                        },
                        "name": "id[灌溉阀的id]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "2": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "name",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "灌溉阀的名称",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "灌溉阀的id"
                        },
                        "name": "name[灌溉阀的名称]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "3": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "if",
                        "field_type": "TEXT",
                        "default_value": "",
                        "ch_name": "控制条件",
                        "index_type": "None",
                        "note": "",
                        "deepcopy_data": {
                            "name": "灌溉阀的名称"
                        },
                        "name": "if[控制条件]",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        }
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "4": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "if_on",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "是否启用控制条件",
                        "index_type": "None",
                        "note": "",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        },
                        "deepcopy_data": {
                            "name": "控制条件"
                        },
                        "name": "if_on[是否启用控制条件]"
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "5": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "type",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "条件成立时 设备处于的状态",
                        "index_type": "None",
                        "note": "",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        },
                        "deepcopy_data": {
                            "name": "是否启用该控制"
                        },
                        "name": "type[条件成立时 设备处于的状态]"
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            },
            "6": {
                "base_info": {
                    "deepcopy_data": {
                        "index": 1,
                        "en_name": "level",
                        "field_type": "INTEGER",
                        "default_value": 0,
                        "ch_name": "控制优先级",
                        "index_type": "None",
                        "note": "",
                        "ps": {
                            "1": {
                                "k": "params1",
                                "v": "value1"
                            }
                        },
                        "deepcopy_data": {
                            "name": "是否启用该控制"
                        },
                        "name": "level[控制优先级]"
                    },
                    "vue_name": "a_1_0_2_29_1",
                    "index": 0
                },
                "dict": {}
            }
        }
    }
}

import inspect
import sys,os,json,time
p=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
p=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(p))))
p=p+os.sep+'_base_tool'+os.sep+'python'
sys.path.append(p)
import sqlite3,yaml,json,copy
import os
import sqlite3
import time
from datetime import datetime
import random
from string import ascii_letters, digits
##############################设置触发器update_add_int  使数据在被更新时 数值自动在区间内递增   step 0/3
set_update_add_int_trigger_val=f'''CREATE TRIGGER update_add_int  
AFTER UPDATE ON ###tablename###  
FOR EACH ROW  
BEGIN  
   UPDATE ###tablename###
   SET   
###val###
   WHERE id = old.id;  
END;'''
set_update_add_int_trigger_val2='''   
      ###a### = CASE  
         WHEN ###a### = ###b### THEN ###c### 
         ELSE ###a### + 1  
      END'''

###############
class base1:
    def cn(self,path):#get_conn_cursor
        conn = sqlite3.connect(path)
        cursor = conn.cursor()
        return cursor,conn
    def create_all_table(self,db_path):
        cur, conn=self.cn(db_path)
        # print('db_path',db_path)
        for item in dir(self):
            if "table__" ==item[:len("table__")]:
                # print("self."+item,'start')
                sql=eval("self."+item)
                try:
                    if "table" not in sql:continue
                except:
                    
                    print('########error1')
                    continue#sql is None :error
                try:
                    # table_name=sql[sql.index("table") + 5:sql.index("(")].strip().replace("IF NOT EXISTS ","")
                    res=cur.execute(sql)
                    conn.commit()  # 将写操作结果提交到数据库
                except Exception as e:
                    print('########error2',e,sql)
        
        for item in dir(self):
            if "index__" ==item[:len("index__")]:
                sql=eval("self."+item)
                try:
                    cur.execute(sql)
                    conn.commit()  # 将写操作结果提交到数据库
                except:pass
            elif "trigger__" ==item[:len("trigger__")]:
                sql=eval("self."+item)
                try:
                    cur.execute(sql)
                    conn.commit()  # 将写操作结果提交到数据库
                except Exception as e:#print('error',e,sql)
                    pass
                # print("create table",table_name,"ok")
        conn.close()
    def delete_all_table(self,db_path):

        cur, conn=self.cn(db_path)
        for item in dir(self):
            if "table_" ==item[:len("table_")]:
                sql = eval("self." + item)
                if "table" not in sql:continue
                sql=sql.replace("IF NOT EXISTS","")
                table_name=sql[sql.index("table") + 5:sql.index("(")].strip()
                delete_sql="DROP TABLE %s ;"%(table_name)
                try:
                    cur.execute(delete_sql)
                    conn.commit()  # 将写操作结果提交到数据库
                    # print("delete table",table_name,"ok")
                except Exception as e:
                    pass
                    # print("delete table",table_name,"error:",e)

        conn.close()
        os.remove(db_path)
        # print("sqlite存储文件已经删除")
    def reset_all_table(self,db_path):
        self.delete_all_table(db_path)
        self.create_all_table(db_path)
    #查找数据,无条件
    def select_info_all(self,cur,conn,table_name,select_field_name=("*")):

        sql="select %s from %s;"%(",".join(select_field_name),table_name)
        # print(sql)

        try:
            cur.execute(sql)
            info=cur.fetchall()#空则为[]
            # conn.commit()
        except:
            info=[]
        conn.close()
        return info
    #查询指定1
    def select_info_from_field(self,cur,conn,table_name,condition_field:dict,select_field_name:list=("*",),limit=None):
        keys, values = zip(*(condition_field.items()))
        sql = "select %s from %s where %s%s;" %(",".join(select_field_name),table_name," and ".join(key+"=?" for key in keys),""if limit is None else (" limit %s"%limit if isinstance(limit,int) else " limit %s,%s"%(limit[0],limit[1])))
        # print(sql)
        try:
            cur.execute(sql,values)
            info = cur.fetchall()  # 空则为[]
            # conn.commit()
        except:
            info = []
        conn.close()
        return info
    #指定时间字段　和limit
    def select_info_from_by_time_and_limit(self,cur,conn,table_name,condition_field:dict=None,limit=(0,10),time_field=("create_time",False),select_field_name:list=("*")):
        values=None
        if condition_field!=None:
            keys, values = zip(*(condition_field.items()))
            sql = "select %s from %s where %s order by datetime(%s) %s limit ?,?;" %(",".join(select_field_name),table_name," and ".join(key+"=?" for key in keys),time_field[0],""if time_field==True else "desc")
        else:
            sql = "select %s from %s  order by datetime(%s) %s limit ?,?;" % (
            ",".join(select_field_name), table_name, time_field[0],
            "" if time_field == True else "desc")
        if values is None:
            values=limit
        else:
            values = list(values)
            values.extend(limit)
        # print(sql)
        try:
            cur.execute(sql,values)
            info = cur.fetchall()  # 空则为[]
            # conn.commit()
        except:
            info = []
        conn.close()
        return info

    #创建数据,[{"key":""}]
    def insert_info_all(self,cur,conn,table_name,info:list,printx=1):
        try:
            for dct in info:
                keys, values = zip(*(dct.items()))
                # print(keys,values)
                sql= "insert into {} ({}) values ({});".format(table_name, ','.join(keys),
                                                          ','.join("?" for _ in range(len(keys)))
                                                             )
                                    
                # print(sql,values)
                cur.execute(sql,values)
            conn.commit()
            conn.close()
            return True
        except Exception as e:
            # print(e)
            conn.close()
            return False
    # 插入所有数据　
    def insert_info_all_lst(self, cur, conn, table_name, info: list):

        try:
            sql = "insert into {} values ({});".format(table_name,
                                                       ','.join("?" for _ in range(len(info)))
                                                       )
            cur.execute(sql, info)
            conn.commit()
        except Exception as e:
            pass
            # print(e)
        conn.close()

    #修改数据
    def update_info(self,cur,conn,table_name,condition_field:dict,change_field:dict):
        try:
            keys2, values2 = zip(*(condition_field.items()))
            keys1, values1 = zip(*(change_field.items()))
            sql= "UPDATE {} SET {} where {};".format(table_name, ",".join(key+"='{}'" for key in keys1), " and ".join(key+"=?" for key in keys2))
            sql=sql.format(*(str(value) for value in values1))
            # print("update",sql)
            cur.execute(sql,values2)
            conn.commit()
        except Exception as e:
            pass
            # print(e)
        conn.close()
    #删除列
    def delete_line(self,cur,conn,table_name,condition_field:dict):
        try:
            keys, values = zip(*(condition_field.items()))
            sql= "delete from {} where {};".format(table_name, " and ".join(key+"=?" for key in keys))
                # print(sql,values)
                # print(sql%values)
            cur.execute(sql,values)
            conn.commit()
        except Exception as e:
            pass
            # print(e)
        conn.close()

    #用户初始化
    def init(s,user_db_path,*args,**kwargs):
        # print('user_db_path',user_db_path)
        s.create_all_table(user_db_path)
        for func in args:
            func(**kwargs)
class base2(base1):
    _number={'0','1','2','3','4','5','6','7','8','9'}
    __isalpha_add='isjksdhf'
    def _replace_key(self,key):
        #替换特殊的字段 如 '1-1' 是不允许使用数字开头 不允许 - ，这里就要进行替换
        key=key.replace('-','_')
        #判断首字符是否是英文，不是的话就添加前缀 self.__isalpha_add
        if key[0].isalpha() is False:
            key= self.__isalpha_add+key
        return key
    def to_json(self,str_):
        try:
            return json.loads(str_)
        except:
            print('$$$','none')
            return ''
    def lambda_f(self,val):
        return lambda :val
    def __init__(self,**kwargs):
        self.test_flag=True
        #获取数据库 建表配置数据
        self.db_data=kwargs.get('db_data',{
                    "base_info": {
                        "index": "5",
                        "vue_name": "a_1_0_2_28_1",
                        "deepcopy_data": {
                            "index": 1,
                            "en_name": "user_db",
                            "ch_name": "用户库",
                            "note": "",
                            "ps": {
                                "1": {
                                    "k": "params1",
                                    "v": "value1"
                                }
                            },
                            "name": "user_db[用户库]"
                        }
                    },
                    "dict": {
                        "1": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "id",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "",
                                    "index_type": "PRIMARY KEY",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "id[]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "2": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "user_name",
                                    "field_type": "TEXT",
                                    "default_value": "",
                                    "ch_name": "用户名",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "user_name[用户名]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "3": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "passwd",
                                    "field_type": "TEXT",
                                    "default_value": "",
                                    "ch_name": "密码",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "passwd[密码]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "4": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "level",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "权限等级",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "level[权限等级]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "5": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "create_id",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "创建人id",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "create_id[创建人id]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        }
                    }
                })
        #获取数据库 建表的表名   没有则获取db_data的键名为表名
        self.table_name=self.db_data['base_info']['deepcopy_data']['en_name']
        #获取数据库 存放数据库文件所在的文件夹 没有则默认为当前文件所在的文件夹
        self.db_dir_path=kwargs.get('db_dir_path',os.path.dirname(os.path.abspath(__file__)))
        #获取数据库 存放数据库文件的具体路径 为存放文件夹+表名+.db
        self.db_file_path=self.db_dir_path+os.sep+self.table_name+'.db'
        # print('self.db_file_path',self.db_file_path)
        #解析配置数据 生成sqlite3的建表数据
        #step -1 根据配置生成字段
        all_field_str=''#存放所有字段
        self.virtual_names='('#创建虚拟数据需要插入字段名
        temp_virtual_names=''
        self.virtual_name_lst=[]
        self.字段输出格式_dct={}
        self.字段输出格式_func={'json':self.to_json}
        self.virtual_types={}#创建虚拟数据 的数据类型
        self.virtual_lens={}#创建虚拟数据的 长度限制范围
        self.default_dct={#数据类型为空时 自动填充内容如下
            'DATETIME':lambda:datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            'INTEGER':lambda :0,
            'TEXT':lambda :'None',
            'VARCHAR':lambda :'None'
        }
        self.id_name=None#主键的名称
        self.field_all_dct={}#存放表内所有字段的 名称 和 类型
        self.field_custom_default={}#默认值
        ##############################设置触发器update_add_int  使数据在被更新时 数值自动在区间内递增   step 1/3
        self.update_add_int=[]
        #############################字段被修改时 自动添加当前字段 和 当前时间  step 0/2
        self.update_modify_time=[]
        for index in self.db_data['dict']:
            field_data=self.db_data['dict'][index]['base_info']["deepcopy_data"]#字段数据
            #字段名称
            key2=field_data['en_name']
            #字段类型
            field_type=field_data['field_type']
            #字段默认值
            default_value=field_data['default_value']
            #索引类型
            index_type=field_data['index_type']

            #保存字段的默认值
            if field_type=="DATETIME":
                self.field_custom_default[key2]=lambda:datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            elif index_type!="PRIMARY KEY":
                self.field_custom_default[key2]=lambda:default_value
            #当前字段添加进所有字段中
            temp_field_str="%s %s %s %s,"%(key2,                                                                                      #字段名称
                                        field_type,                                                                    #字段类型
                                        'PRIMARY KEY' if index_type=='PRIMARY KEY' else '',                                               #是否时主键
                                        'autoincrement' if (index_type=='PRIMARY KEY' and field_type=="INTEGER") else '', 
                                        )
            #自增长
            if (index_type=='PRIMARY KEY' and field_type=="INTEGER"):
                temp_virtual_names+='%s,'%key2
                self.virtual_types[key2]=field_type
                self.virtual_lens[key2]=''
                self.virtual_name_lst.append(key2)
            #主键
            if index_type=='PRIMARY KEY':
                self.id_name=key2
            # if temp_field_dct.get('字段输出格式') is not None:
            #     self.字段输出格式_dct[key2]=temp_field_dct['字段输出格式']
            all_field_str+=temp_field_str
            
            
    #   <el-select v-model="form.index_type" placeholder="请选择索引类型">
    #     <el-option label="None【无】" value="None"></el-option>
    #     <el-option label="PRIMARY KEY【主键】" value="PRIMARY KEY"></el-option>
    #     <el-option label="Unique Index【唯一索引】" value="Unique Index"></el-option>
    #     <el-option label="Index【索引】" value="Index"></el-option>
            #普通索引
            if index_type=='Index':exec('self.index__%s=%s'%(key2,'"create index index__%s on %s(%s);"'%(key2,self.table_name,key2)))
            #唯一索引
            if index_type=="Unique Index" :exec('self.index__%s=%s'%(key2,'"create unique index index__%s on %s(%s);"'%(key2,self.table_name,key2)))
            #如果是时间类型
            ##############################字段被修改时 自动添加当前字段 和 当前时间  step 1/2
            if default_value=='update_time' and field_type=="DATETIME":
                self.update_modify_time.append(key2)#datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        self.filed_name_lst=list(self.field_custom_default.keys())
        self.insert_sql="insert into {} ({}) values ({});".format(self.table_name,','.join(self.filed_name_lst),','.join("?" for _ in range(len(self.filed_name_lst))))
        if all_field_str[-1]==',':all_field_str=all_field_str[:-1]#去除最后的,
        # print(all_field_str)
        try:
            if temp_virtual_names[-1]==',':temp_virtual_names=temp_virtual_names[:-1]#去除最后的,
            self.virtual_names=self.virtual_names+temp_virtual_names+')'
            #创建表 的建表信息
            self.table__info="""create table IF NOT EXISTS %s(%s);"""%(self.table_name,all_field_str)
        except:
            self.table__info=None,"没有设置主键primary key"
        
if __name__ == '__main__':

    config={"base_info": {
        "index": 2
    },
    "dict": {
        "1": {
            "base_info": {
                "vue_name": "a_1_0_2_26_1",
                "index": "1",
                "deepcopy_data": {
                    "name": "数据库列表"
                }
            },
            "dict": {
                "1": {
                    "base_info": {
                        "index": "5",
                        "vue_name": "a_1_0_2_28_1",
                        "deepcopy_data": {
                            "index": 1,
                            "en_name": "user_db",
                            "ch_name": "用户库",
                            "note": "",
                            "ps": {
                                "1": {
                                    "k": "params1",
                                    "v": "value1"
                                }
                            },
                            "name": "user_db[用户库]"
                        }
                    },
                    "dict": {
                        "1": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "id",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "",
                                    "index_type": "PRIMARY KEY",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "id[]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "2": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "user_name",
                                    "field_type": "TEXT",
                                    "default_value": "",
                                    "ch_name": "用户名",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "user_name[用户名]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "3": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "passwd",
                                    "field_type": "TEXT",
                                    "default_value": "",
                                    "ch_name": "密码",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "passwd[密码]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "4": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "level",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "权限等级",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "level[权限等级]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        },
                        "5": {
                            "base_info": {
                                "deepcopy_data": {
                                    "index": 1,
                                    "en_name": "create_id",
                                    "field_type": "INTEGER",
                                    "default_value": 0,
                                    "ch_name": "创建人id",
                                    "index_type": "None",
                                    "note": "",
                                    "ps": {
                                        "1": {
                                            "k": "params1",
                                            "v": "value1"
                                        }
                                    },
                                    "name": "create_id[创建人id]"
                                },
                                "vue_name": "a_1_0_2_29_1",
                                "index": 0
                            },
                            "dict": {}
                        }
                    }
                }
            }
        },
        "2": {
            "base_info": {
                "vue_name": "a_1_0_2_27_1",
                "index": 0,
                "deepcopy_data": {
                    "name": "搜索数据库"
                }
            },
            "dict": {}
        }
    }
}
    #取出来color_db的数据
    for key in test:
        obj=base2(db_data=test[key])
        print(obj.table__info)
    
